package Lesson;

import Homework.Commodity;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapTest {
    public static void main(String[] args) {
        Map<Integer, Commodity> map = new HashMap<>();
        //添加元素
        map.put(1,new Commodity(1,"电视","1200","深圳","2021-07-01"));
        map.put(5,new Commodity(5,"电饭锅","700","成都","2020-08-01"));
        map.put(2,new Commodity(2,"电磁炉","800","北京","2021-04-06"));
        map.put(8,new Commodity(8,"电风扇","500","杭州","2020-11-12"));
        map.put(6,new Commodity(6,"豆浆机","1000","宁波","2021-11-15"));

        //根据键得到值，没有返回null
        Commodity c = map.get(2);
        System.out.println(c);

        //得到集合长度
        int len = map.size();
        System.out.println(len);

        //判断集合中是否存在指定的键
        boolean isContain = map.containsKey(8);
        System.out.println(isContain);


        //遍历
        //得到map集合的值集合
       Collection<Commodity> valueList = map.values();
       for (Commodity a:valueList){
           System.out.println(a);
       }

       //得到键的集合
       Set<Integer> set = map.keySet();
       for (Integer key:set){
           System.out.println(key+"   "+map.get(key));
       }

       map.remove(8);
        for (Integer key:set){
            System.out.println(key+"   "+map.get(key));
        }

    }
}
